<form  class="layui-form layui-card" action="{:request()->url()}" data-auto="true" method="post" autocomplete="off">

    <div class="layui-card-body">
        <div class="layui-form-item">

            {if @$vo.id}
            <label class="layui-form-label label-required">访客姓名</label>
            {else}
            <label class="layui-form-label label-required">访客姓名</label>
            {/if}
            <div class="layui-input-block">
                <input name="name" value='' required placeholder="访客姓名" class="layui-input">
            </div>
        </div>

        
        <div class="layui-form-item">

           
            <label class="layui-form-label">访客身份证</label>
          
            <div class="layui-input-block">
                {if @$vo.code}
                    <input readonly name="code" value='{$companyname[0]["code"]|default=""}' placeholder="访客身份证" class="layui-input">
                {else}
                    <input readonly name="code" value='' placeholder="访客身份证" class="layui-input">
                {/if}
            </div>
        </div>
<!--        
        <div class="layui-form-item">

            {if @$vo.address}
            <label class="layui-form-label label-required">到访公司</label>
            {else}
            <label class="layui-form-label label-required">到访公司</label>
            {/if}
            <div class="layui-input-block">
                <input name="address" value='{$companyname[0]["address"]|default=""}' required placeholder="请输入公司地址" class="layui-input">
            </div>
        </div> -->

            <div class="layui-form-item">

              <label class="layui-form-label label-required">到访公司</label>
            <div class="layui-input-block">
                <input style="position: absolute;z-index:2;width: 80%;" autocomplete="off" name="companyname" value='' required placeholder="请输入公司名称" class="layui-input" id="searchcompany">
                <select id="companylist"  class="layui-select" name="companyid" lay-filter="companyid">
                 {if (@$vo.company_name)}
                  <option value="{$vo.companyid}">{$vo.company_name}</option>
                 {else}
                  <option value="">-- 选择公司 --</option>
                 {/if} 
               

                    {foreach  $data['companylist'] as $key=>$value}
                         {if($value.id == @$vo.id )}
                    
                        <option selected value="{$value.id}">{$value.name}</option>
                        {else}
                        <option value="{$value.id}">{$value.name}</option>
                      
                        {/if}
                    {/foreach}
                </select>
                <div style="display: none;">
                <select id="companylist-bck"  class="layui-select" name="companyid_bck" lay-filter="companyid_bck">
                 {if (@$vo.company_name)}
                  <option value="{$vo.companyid}">{$vo.company_name}</option>
                 {else}
                  <option value="">-- 选择公司 --</option>
                 {/if} 
               

                    {foreach  $data['companylist'] as $key=>$value}
                         {if($value.id == @$vo.id )}
                    
                        <option selected value="{$value.id}">{$value.name}</option>
                        {else}
                        <option value="{$value.id}">{$value.name}</option>
                      
                        {/if}
                    {/foreach}
                </select>
              </div>


            </div>
           

        </div>

                <div class="layui-form-item">

            <label class="layui-form-label label-required">到访部门</label>
            <div class="layui-input-block">

            <select id="deptlist"  class="layui-select" name="departmentid" lay-filter="departmentid">
                <option value="">-- 选择部门 --</option>

                {foreach $data['deptlist'] as $key=>$val}

                {if @$vo.deptid}
                    <option data-is_security="{$val.is_security}" selected value="{$val.id}">{$val.dept_name}</option>
                {else}
                    <option data-is_security="{$val.is_security}" value="{$val.id}">{$val.dept_name}</option>
                  
                {/if}
                {/foreach}
            </select>
            </div>

        </div>

    </div>




    <div class="hr-line-dashed"></div>
    {notempty name='vo.id'}<input type='hidden' value='{$vo.id}' name='id'>{/notempty}

    <div class="layui-form-item text-center">
        <button class="layui-btn" type='submit'>确 定</button>
        <button class="layui-btn layui-btn-danger" type='button' data-confirm="确定要取消编辑吗？" data-close>取消编辑</button>
    </div>


</form>


{block name='script'}

<script>

    require(['angular', 'ckeditor','selectM','selectN'], function () {

        var $form = $('form[name="news"]');
        var $vali = $form.vali().data('validate');
        // var editor = window.createEditor('[name="content"]');

        var app = angular.module("NewsEditor", []).run(callback);
        angular.bootstrap(document.getElementById(app.name), [app.name]);

        /**搜索公司**/
        $("#searchcompany").on("input",function(e){
            $("#companylist").next().find("dl").css({ "display": "block" });
            $('#companylist').html('')
            form.render('select')

            if(e.delegateTarget.value==''){
              /**空值恢复**/
              var bckoption = $("#companylist-bck").html()
              console.log(bckoption)
              $('#companylist').html('')
              $('#companylist').append(bckoption)
              form.render('select')
              return;

            }

            $.ajax({
              url:"{:url('company/company/getlist')}",
              data:{name:e.delegateTarget.value},
              type:'post',
              async: false,
              dataType:'json',
              success:function(res) {
                var reres = JSON.parse(res)
                var option = ''
                for (var i = 0; i < reres.data.length; i++) {
                    option = option+"<option value="+reres.data[i].id+">"+reres.data[i].name+"</option>"
                }
                console.log(reres)
                $('#companylist').append(option)
              }
          })
            form.render('select')
        });


        /**选择公司后搜搜部门**/
        form.on('select(companyid)', function(data){
            var companyname_select = $('#companylist option:selected').text()
            $('input[name="companyname"]').val(companyname_select)

            $('#deptlist').html('')
            form.render('select')
            $.ajax({
              url:"{:url('company/department/getdeptlist')}",
              data:{companyid:data.value},
              type:'post',
              async: false,
              dataType:'json',
              success:function(res) {
                var reres = JSON.parse(res)
                var option = ''
                console.log(reres)
                for (var i = 0; i < reres.data.length; i++) {
                    option = option+"<option data-is_security="+reres.data[i].is_security+" value="+reres.data[i].id+">"+reres.data[i].dept_name+"</option>"
                }
                $('#deptlist').append(option)

              }
          }) 
        form.render('select')
      });

        /**选择部门后**/
        form.on('select(departmentid)', function(data){
            console.log(data)
            var is_security = $(data.elem).find("option:selected").attr("data-is_security")
            console.log(is_security)
            console.log(data.elem[data.elem.selectedIndex]); 
            if(is_security==1){
                $('input[name="is_security"]').val(1)   
            }else{
                $('input[name="is_security"]').val(0) 
            }
            console.log($('input[name="is_security"]').val());
      });

        /**选择手机后**/
        form.on('select(watchphone)', function(data){
          console.log(data)
              $.ajax({
                url:"{:url('company/user/getuserlist')}",
                data:{id:data.value},
                type:'post',
                async: false,
                dataType:'json',
                success:function(res) {
                  var reres = JSON.parse(res)
                  console.log(reres)
                  if(reres.data.roleid==2){
                    $('.authorize-changer').css({'display':'block'})
                  }else{
                    $('.authorize-changer').css({'display':'none'})
                    $('input[name="ch_role"]').val('off');
                  }
                  $('#searchname').val(reres.data.name)
                  $("input[name='mobile']").val(reres.data.mobile)
                  $("input[name='userid']").val(reres.data.id)
                }
            })
          });


        /**搜索电话监听**/
        $("#searchphone").on("input",function(e){
            $("#searchphonelist").next().find("dl").css({ "display": "block" });
            console.log(e.delegateTarget.value);
            $('#searchphonelist').html('')
            form.render('select')
            if(e.delegateTarget.value==''){return;}
            $.ajax({
              url:"{:url('company/user/getuserlist')}",
              data:{'phone':e.delegateTarget.value,'type':'phone'},
              type:'post',
              async: false,
              dataType:'json',
              success:function(res) {
                console.log(res)
                var reres = JSON.parse(res)
                var option = ''
                for (var i = 0; i < reres.data.length; i++) {
                    option = option+"<option value="+reres.data[i].id+">"+reres.data[i].mobile+"</option>"
                }
                console.log(reres)
                $('#searchphonelist').append(option)
              }
          })
            form.render('select')
        });

        function callback($rootScope) {
            $rootScope.list = [];
            $rootScope.item = {};

            $rootScope.setItemValue = function (name, value) {
                $rootScope.item[name] = value;
                $rootScope.item.style = "background-image:url('" + $rootScope.item.local_url + "')";



                // $.post("{:url('company/company/upload')}", {'local_url':value}, function(result) {
                 
                //   result = JSON.parse(result)
                //   console.log(result)
                //   if (result.code == 200) {
                //       //成功
                //       $('input[name="name"]').val(result.data.name)
                //       $('input[name="code"]').val(result.data.code)
                //       $('input[name="address"]').val(result.data.address)
                //       $('input[name="corporation"]').val(result.data.corporation)
                //       return $.msg.success(result.message);
                //   } else if (result.code == 400) {
                //       // 失败
                //       return $.msg.error(result.message);
                //   }
                //       }, "JSON");
            };


            // $('[name="local_url"]').on('change', function () {
            //     var value = this.value;
            //     $rootScope.$apply(function () {
            //         $rootScope.setItemValue('local_url', value);
            //     });
            // });
        }
    });
</script>
<script type="text/javascript">

// window.form.render();
form.render()
require(['formSelects'], function () {
var formSelects = layui.formSelects;

});
</script>

{/block}